package com.kitty.game.task.service.taskHandler;

import com.kitty.game.role.model.Role;
import com.kitty.game.config.ShapePen;
import com.kitty.game.config.TaskSet;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.task.TaskDataPool;
import com.kitty.game.task.message.vo.TaskInfo;
import com.kitty.game.task.model.product.TaskType;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.utils.FieldUtils;
import com.kitty.game.utils.TimeUtil;
import com.kitty.mina.message.MessagePusher;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;


/**
 * 九曲玲珑笔
 */
@Component
public class ShapePenTaskTaskHandler extends TimeTaskHandler {
    private static final String EXPIRE_TIPS = "九曲玲珑笔{0}已经过期，系统自动清除。";

    @Override
    public TaskType getTaskType() {
        return TaskType.SHAPE_PEN;
    }

    @Override
    public TaskInfo getTaskInfo(Role role, TaskSet taskSet) {
        ShapePen shapePen = role.getShapePen();
        if (shapePen == null) {return null;}
        TaskInfo taskInfo = super.getTaskInfo(role, taskSet);
        taskInfo.setTask_end_time(shapePen.getExpireTime());
        taskInfo.setRefresh((short) 1);
        taskInfo.setTaskZhiyin(MessageFormat.format(taskSet.getTaskZhiyin(), shapePen.getName()));
        taskInfo.setTaskInfo(MessageFormat.format(taskSet.getTaskJieshao(), shapePen.getName()));
        return taskInfo;
    }

    public boolean acceptTask(Role role) {
        TaskSet taskSet = taskService.getTaskSet(TaskDataPool.SHAPE_PEN_TASKID, role);
        return acceptTask(role, taskSet);
    }

    public void delTask(Role role) {
        TaskSet taskSet = taskService.getTaskSet(TaskDataPool.SHAPE_PEN_TASKID, role);
        /**任务已经接取时，删除原任务再添加一个*/
        if (taskService.isTaskAccepted(role, taskSet)) {
            giveUpTask(role, taskSet);
        }
    }

    @Override
    public void doBeforeAccept(Role role, TaskSet taskSet) {
        super.pushEndTask(role, taskService.getTaskSet(TaskDataPool.SHAPE_PEN_TASKID, role));
    }

    /**
     * 能否放弃任务
     */
    @Override
    public boolean canGiveUpTask(Role role, TaskSet taskSet) {
        return true;
    }

    /**
     * 处理任务倒计时
     */
    @Override
    public void handleHeartBeat(Role role, long prevHeartTime) {
        TaskSet taskSet = taskService.getTaskSet(TaskDataPool.SHAPE_PEN_TASKID, role);
        /**如果有任务就继续下面*/
        if (taskService.isTaskAccepted(role, taskSet)) {
            ShapePen shapePen = role.getShapePen();
            if (shapePen != null) {
                long expireTime = shapePen.getExpireTime();
                long currTime = System.currentTimeMillis();
                if (expireTime - TimeUtil.ONE_SECOND < (currTime/1000)) {
                    giveUpTask(role, taskSet);
                    MessagePusher.pushMessage(role, new RespMsg(MessageFormat.format(EXPIRE_TIPS, shapePen.getName())));
                }
            }
        }
    }

    /**
     * 进行任务放弃前的操作
     */
    @Override
    public void doBeforeGiveUpTask(Role role, TaskSet taskSet) {
        /**放弃任务 删除变身卡 刷新外观 更新增益属性*/
        role.setShapePen(null);

        FieldUtils.sendChangeIcon(role);
        /**更新外观*/
        SpringUtils.getBroadcastService().sendUpdateAppear(role);

        /**刷新面板*/
        SpringUtils.getRoleService().sendExtraAttribute(role);

    }

}
